﻿var grid, cbProgress;

//初始化表格
function InitGrid() {
    var txtGoodsCode = $('#txtGoodsCode').val();
    var txtGoodsName = $('#txtGoodsName').val();
    var lookupType = $('#lookupType').combobox('getValue');
    grid = $('#tab_list').datagrid({
        name: 'tbl_data',
        url: '/GoodsInfoManage/InitJsonResult',
        title: '数据信息',
        queryParams: {
            goodsCode: txtGoodsCode,
            goodsName: txtGoodsName,
            lookup: lookupType
        },
        //width: 815,
        height: 530,
        //border : false,
        //fit: false,
        fitColumns: false,
        //nowrap: true,
        rownumbers: true,
        //showFooter: true,
        //loadMsg: '正在加载信息...',
        //frozen: true,
        //sortName: "CODE", //排序设置
        singleSelect: true,
        idField: 'GOODSID',
        columns: [[
        //{ field: 'ck', checkbox: true },
                    { field: 'GOODSID', title: 'GOODSID', align: 'center', sortable: true },
                    { field: 'GOODSCODE', title: '货号', align: 'center', sortable: true },
                    { field: 'GOODSNAME', title: '品名', align: 'center', sortable: true },
                    { field: 'PKGLOSSQTY', title: '净含量', align: 'center', sortable: true },
                    { field: 'SALESTOCKQTY', title: '可销售库存占比', align: 'center', sortable: true },
                    { field: 'LOOKUP', title: '库位', align: 'center', sortable: true },
                    { field: 'DLOOKUP', title: '自定义库位', align: 'center', sortable: true }
        ]],
        toolbar: [{
            id: 'btnEdit',
            text: '修改',
            iconCls: 'icon-edit',
            handler: function () {
                edit();
            }
        }],
        pagination: true,
        pageSize: 20,
        pageNumber: 1,
        pageList: [10, 20, 30, 40, 50],
        rownumbers: true, //行号
        onLoadSuccess: function (data) {
            onRefreshButton();
        },
        onSelect: function (rowIndex, rowData) {
            onRefreshButton();
        },
        onUnselect: function (rowIndex, rowData) {
            onRefreshButton();
        },
        onSortColumn: function (sort, order) {
            grid.datagrid('reload');
        }
    });
    //清空选择
    grid.datagrid('clearSelections');
}
function formatBool(val) {
    if (val != null) {
        if (val == "1") {
            return "启用";
        } else {
            return "禁用";
        }
    }
}


//控制按钮
function onRefreshButton() {
    $('#btnEdit').linkbutton('disable');

    var rows = grid.datagrid('getSelections');
    var count = rows.length;
    if (rows.length == 1) {
        $('#btnEdit').linkbutton('enable');
    }
}

//增加
function edit() {
    var rows = grid.datagrid('getSelections');
    $('#CustomCELLNO').combobox('setValue', '0');
    rows = grid.datagrid('getSelections');
    $('#dlg').dialog('open').dialog('setTitle', '修改商品资料信息');
    $("#txtGOODSCODE").val(rows[0].GOODSCODE);
    $("#txtGOODSNAME").val(rows[0].GOODSNAME);
    $('#lookupTypes').combobox('setValue', rows[0].CELLNO);
    $("#txtGoodsJhl").val(rows[0].PKGLOSSQTY);
    $("#txtSaleStockQTY").val(rows[0].SALESTOCKQTY);
    $('#hiddenGoodsCode').val(rows[0].GOODSCODE);


    $("#txtGOODSCODE").attr("disabled", "disabled");
    $("#txtGOODSNAME").attr("disabled", "disabled");
    $('#lookupTypes').combobox('disable');
    url = '/GoodsInfoManage/SaveData';
}

function saveData() {

    $('#location').form('submit', {
        url: '/GoodsInfoManage/SaveData',
        success: function (data) {
            eval('data=' + data);
            if (data.success) {
                $('#dlg').dialog('close');
                InitGrid();
            } else {
                $.messager.alert('错误', data.msg, 'error');
            }
        }
    });
}

